package com.blrs.service.product.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.common.utils.uuid.UUID;
import com.blrs.domain.PolicyReception;
import com.blrs.domain.PolicyRelease;
import com.blrs.domain.ProductReception;
import com.blrs.mapper.EnterpriseInfoMapper;
import com.blrs.mapper.ProductReceptionMapper;
import com.blrs.service.product.IProductReleaseService;
import com.blrs.system.mapper.SysDeptMapper;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.ProductReleaseMapper;
import com.blrs.domain.ProductRelease;

/**
 * 产品发布Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-10
 */
@Service
public class ProductReleaseServiceImpl implements IProductReleaseService
{
    @Autowired
    private ProductReleaseMapper productReleaseMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private ProductReceptionMapper receptionMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询产品发布
     * 
     * @param productId 产品发布主键
     * @return 产品发布
     */
    @Override
    public ProductRelease selectProductReleaseByProductId(String productId)
    {
        return productReleaseMapper.selectProductReleaseByProductId(productId);
    }

    /**
     * 查询产品发布列表
     * 
     * @param productRelease 产品发布
     * @return 产品发布
     */
    @Override
    public List<ProductRelease> selectProductReleaseList(ProductRelease productRelease)
    {
        if (SecurityUtils.getUserId()!=1 && SecurityUtils.getUserId()!=2){
            productRelease.setBelongTo(SecurityUtils.getOrgId());
        }
        List<ProductRelease> productReleases = productReleaseMapper.selectProductReleaseList(productRelease);
        for (ProductRelease release : productReleases) {
            release.setUrl( sysUploadFileMapper.selectUrlByCode("productRelease",release.getUploadFile()));
        }
        return productReleases;
    }

    /**
     * 新增产品发布
     * 
     * @param productRelease 产品发布
     * @return 结果
     */
    @Override
    public int insertProductRelease(ProductRelease productRelease) throws NoSuchFieldException, IllegalAccessException {
        productRelease.setProductId(UUID.randomUUID().toString().replaceAll("-", ""));
        if (productRelease.getSendType().equals("groupSend")){
            for (String s : productRelease.getEnterpriseId()) {
                if (StringUtils.isNotEmpty(s)){
                    //先查询对应组织机构id
                    ProductReception productReception = new ProductReception();
                    //根据企业id查询企业名称
                    String enterpriseName = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(s).getEnterpriseName();
                    //根据企业名称查询组织机构id
                    productReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
                    productReception.setEnterpriseId(s);
                    productReception.setProductId(productRelease.getProductId());
                    receptionMapper.insertProductReception(productReception);
                }
            }
        }else {
            //说明是一键发送
            //获取所有的入驻企业id
            List<SysDept> deptList = sysDeptMapper.selectOrgList(null);
            List<ProductReception> receptionList = new ArrayList<>();

            for (SysDept sysDept : deptList) {
                ProductReception productReception = new ProductReception();
                productReception.setProductId(productRelease.getProductId());
                productReception.setReceiverId(sysDept.getDeptId());
                receptionList.add(productReception);
            }
// 批量插入
            receptionMapper.batchInsertProductReception(receptionList);

        }
        //状态
        productRelease.setStatus("审核");
        productRelease.setReceiverId(String.join(",",productRelease.getEnterpriseId()));
        productRelease.setSendUnit(SecurityUtils.getOrgName());
        ReflectionUtils.setCommonPrivateProperty(productRelease);
        productRelease.setBelongTo(SecurityUtils.getOrgId());
        productRelease.setOrganizationName(SecurityUtils.getOrgName());
        return productReleaseMapper.insertProductRelease(productRelease);
    }

    /**
     * 修改产品发布
     * 
     * @param productRelease 产品发布
     * @return 结果
     */
    @Override
    public int updateProductRelease(ProductRelease productRelease)
    {
        //判断传入的是一键发送还是分类群发
        if (productRelease.getSendType().equals("groupSend")){
            //说明是分类群发,删除旧的接收表数据，添加新的
            receptionMapper.deleteProductReceptionByProductId(productRelease.getProductId());
            for (String s : productRelease.getEnterpriseId()) {
                if (StringUtils.isNotEmpty(s)) {
                    //先查询对应组织机构id
                    ProductReception productReception = new ProductReception();
                    //根据企业id查询企业名称
                    String enterpriseName = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(s).getEnterpriseName();
                    //根据企业名称查询组织机构id
                    productReception.setReceiverId(sysDeptMapper.selectByDeptName(enterpriseName).getDeptId());
                    productReception.setEnterpriseId(s);
                    productReception.setProductId(productRelease.getProductId());
                    receptionMapper.insertProductReception(productReception);
                }
            }
        }else {
            //说明是一键发送
            if (productReleaseMapper.selectIsSend(productRelease.getProductId()).equals("groupSend")){
                //说明修改之前为分类群发
                receptionMapper.deleteProductReceptionByProductId(productRelease.getProductId());
                //获取所有的入驻企业id
                List<SysDept> deptList = sysDeptMapper.selectOrgList(null);
                List<ProductReception> receptionList = new ArrayList<>();

                for (SysDept sysDept : deptList) {
                    ProductReception productReception = new ProductReception();
                    productReception.setProductId(productRelease.getProductId());
                    productReception.setReceiverId(sysDept.getDeptId());
                    receptionList.add(productReception);
                }
// 批量插入
                receptionMapper.batchInsertProductReception(receptionList);
            }
        }
        return productReleaseMapper.updateProductRelease(productRelease);
    }

    /**
     * 批量删除产品发布
     * 
     * @param productIds 需要删除的产品发布主键
     * @return 结果
     */
    @Override
    public int deleteProductReleaseByProductIds(String[] productIds)
    {
        return productReleaseMapper.deleteProductReleaseByProductIds(productIds);
    }

    /**
     * 删除产品发布信息
     * 
     * @param productId 产品发布主键
     * @return 结果
     */
    @Override
    public int deleteProductReleaseByProductId(String productId)
    {
        return productReleaseMapper.deleteProductReleaseByProductId(productId);
    }

    @Override
    public List<ProductRelease> selectProductReleaseListByExamine(ProductRelease productRelease) {
        if (SecurityUtils.getOrgId().equals(100L)) {
            productRelease.setSendType("oneClick");
            List<ProductRelease> productReleases = productReleaseMapper.selectProductReleaseList(productRelease);
            for (ProductRelease release : productReleases) {
                release.setUrl(sysUploadFileMapper.selectUrlByCode("productRelease", release.getUploadFile()));
            }
            return productReleases;
        } else {
          //说明是非平台人员，查询分类群发数据,并且是本公司自己的数据
            productRelease.setSendType("groupSend");
            productRelease.setBelongTo(SecurityUtils.getOrgId());
            productRelease.setExamineId(SecurityUtils.getUserId());
            List<ProductRelease> productReleases = productReleaseMapper.selectProductReleaseList(productRelease);
            for (ProductRelease release : productReleases) {
                release.setUrl(sysUploadFileMapper.selectUrlByCode("productRelease", release.getUploadFile()));
            }
            return productReleases;
        }
    }

    @Override
    public int updateProductReleaseByStatus(String id) {
        return productReleaseMapper.updateProductReleaseByStatus(id);
    }

    @Override
    public List<ProductRelease> selectProductReleaseListReceive(ProductRelease productRelease) {
        productRelease.setBelongTo(SecurityUtils.getOrgId());
        List<ProductRelease> policyReleaseList=productReleaseMapper.selectPolicyReleaseListByReceiveId(productRelease);
        for (ProductRelease release : policyReleaseList) {
            release.setUrl(sysUploadFileMapper.selectUrlByCode("productRelease",release.getUploadFile()));
        }
        return policyReleaseList;
    }

    @Override
    public int terminationPolicy(ProductRelease productRelease) {
        receptionMapper.deleteProductReceptionByProductId(productRelease.getProductId());
        return productReleaseMapper.updateProductReleaseByEnd(productRelease);
    }
}
